package com.pengtu.gsj.controller.afgc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.pengtu.gsj.controller.base.DefaultBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.entity.afgc.LifeCherish;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.app.User;
import com.pengtu.gsj.service.afgc.LifeCherishService;
import com.pengtu.gsj.service.system.SysmenuManagerService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.FileUtils;
import com.pengtu.gsj.utils.UserUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

@Controller
@RequestMapping("lifeProject")
public class LifeCherishController extends DefaultBaseController{
	
	@Autowired
	private LifeCherishService lifeCherishService;
	@Autowired
	private SysmenuManagerService sysmenuManagerService;
	@Autowired
	private SystemManageService systemManageService;
	
	
	@ModelAttribute
	public LifeCherish load(String id){
		LifeCherish lifeCherish;
		if (StringUtils.isNotEmpty(id)) {
			lifeCherish = lifeCherishService.getLifeCherishById(id);
		} else {
			lifeCherish = new LifeCherish();
		}
		return lifeCherish;
	}
	
	/**
	 * 
	 * firstListInfo:
	 * 适用: 跳转到一二级安全生命防护工程列表页面
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("listInfo")
	public String firstListInfo() {
		return "afgc/firstLifeCherish_list";
	}
	
	/**
	 * 
	 * gisFirstView:
	 * 适用:从gis入口查看安防工程的详细信息 
	 * @param lifeCherish
	 * @return  根据查看的项目的等级  跳转到不同的查看页面
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("gisView")
	public String gisFirstView(@ModelAttribute LifeCherish lifeCherish) {
		System.out.println("size:"+lifeCherish.getAttachments().size());
		//根据技术等级的不同 跳转到不同的查看页面
		if ("1".equals(lifeCherish.getTechnicalLevel()) || "2".equals(lifeCherish.getTechnicalLevel())) {
			return "afgc/gisFirstLifeCherishView";
		} else {
			return "afgc/gisThirdLifeCherishView";
		}
	}
	
	/**
	 * 
	 * gisFirstEditor:
	 * 适用: 从gis入口处理工程
	 * @param lifeCherish
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("gisEditor")
	public String gisFirstEditor(@ModelAttribute LifeCherish lifeCherish) {
		//根据技术等级的不同 跳转到不同的查看页面
			return "afgc/gisFirstLifeCherishEditor";
	}
	
	/**
	 * 
	 * thirdListInfo:
	 * 适用:跳转到三四级安全生命防护工程页面
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("thirdListInfo")
	public String thirdListInfo() {
		return "afgc/thirdLifeCherish_list";
	}
	
	/**
	 * 
	 * download:
	 * 适用:下载安全生命防护工程导入表格模板
	 * @param request
	 * @return
	 * @throws IOException 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("download")
	@ResponseBody
	public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException {
		String type = SpringMvcUtils.getParameter("type");
		String rootPath;
		//根据type的值,下载不同的模板
		if (type.equals("1")) {
			rootPath = SpringMvcUtils.getServletContext().getRealPath("/resources/templet/一二级安防工程导入模板.xlsx");
			return FileUtils.downFile(rootPath, "一二级安防工程导入模板.xlsx");
		} else {
			rootPath = SpringMvcUtils.getServletContext().getRealPath("/resources/templet/三四级安防工程导入模板.xlsx");
			return FileUtils.downFile(rootPath, "三四级安防工程导入模板.xlsx");
		}
		
	}
	
	@ResponseBody
	@RequestMapping("fileDown")
	public ResponseEntity<byte[]> downAllFile(@ModelAttribute LifeCherish lifeCherish) throws IOException{
		return lifeCherishService.downAllFiles(lifeCherish);
	}
	
	/**
	 * 
	 * downloadTemp:
	 * 适用: 在导入的时候如果数据出错，将文件提供给用户，供下载使用
	 * @return
	 * @throws IOException 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("downloadTemp")
	@ResponseBody
	public ResponseEntity<byte[]> downloadTemp(HttpServletRequest request) throws IOException {
		String type = SpringMvcUtils.getParameter("type");
		String fileName = SpringMvcUtils.getParameter("fileName");
		String rootPath = System.getProperty("java.io.tmpdir")+"gsjTemp\\"+fileName; //临时目录
		if (type.equals("1")) {
			return FileUtils.downFile(rootPath, "一二级安防工程汇总表.xlsx");
		} else {
			return FileUtils.downFile(rootPath, "三四级安防工程汇总表.xlsx");
		}
		
	}
	
	/**
	 * 
	 * importHigh:
	 * 适用:导入一二级安全生命防护工程数据
	 * @param multipartFile  上传的表格文件
	 * @param request
	 * @return  返回导入结果集合
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value = "importHigh", method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> importHigh(@RequestParam(value = "file", required = false) MultipartFile multipartFile, HttpServletRequest request) throws Exception {
		String type = SpringMvcUtils.getParameter("type");
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if (StringUtils.isNotEmpty(type)) {
			list = lifeCherishService.importHigherProject(multipartFile, type);
		} 
		return list;
	}
	
	/**
	 * 
	 * list:
	 * 适用:得到一二级安全生命防护工程数据 根据状态过滤
	 * @param page
	 * @param request
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("list")
	@ResponseBody
	public Page<LifeCherish> list(Page<LifeCherish> page, HttpServletRequest request) {
		String delStatus = SpringMvcUtils.getParameter("delStatus");
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!StringUtils.isEmpty(delStatus)) {
			queryFilter.addFilter("delStatus", delStatus,MatchType.EQ);
		}
		//如果是辖区局角色  对数据进行过滤
		if (UserUtils.hasXqjRole()) {
			User user = UserUtils.getUser();
			if (user != null) {
				queryFilter.addFilter("county", user.getOrg().getOrgNum(), MatchType.EQ);
			}
		}
		if (!page.isOrderBySetted()) {
			page.setOrderBy("roadCode");
			page.setOrder(Page.ASC);
		}
		page = lifeCherishService.findLifeCherish(page, queryFilter);
		return page;
	}
	
	/**
	 * 
	 * showLifeCherish:
	 * 适用:ajax请求获取工程对象数据
	 * @param lifeCherish
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("show")
	@ResponseBody
	public LifeCherish showLifeCherish(@ModelAttribute LifeCherish lifeCherish) {
		/*状态为未处置状态时，修改状态,并记录操作记录*/
		if (lifeCherish.getCurrentStatus().equals("0")) {
			lifeCherish.setCurrentStatus("1");
			lifeCherish.setDelStatus("1");
			String time = DateUtils.convertDateToString(DateUtils.dayTimePattern, new Date());
			String userName = UserUtils.getName();
			lifeCherish.setOperation("<b>"+time+", 由 "+userName+" 处置了工程;</b><br/>");
		}
		lifeCherishService.saveLifeCherish(lifeCherish);
		return lifeCherish;
	}
	
	@RequestMapping("showView")
	@ResponseBody
	public Map<String, Object> showViewLifeCherish(@ModelAttribute LifeCherish lifeCherish) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("entity", lifeCherish);
		List<Attachment> attachments = systemManageService.getAttachmentBy("tableId", lifeCherish.getId());
		map.put("attachments", attachments);
		return map;
	}
	
	/**
	 * 
	 * saveLifeCherish:
	 * 适用:ajax保存工程数据
	 * @param lifeCherish
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value = "save", method=RequestMethod.POST)
	@ResponseBody
	public LifeCherish saveLifeCherish(@ModelAttribute LifeCherish lifeCherish) {
		String time = DateUtils.convertDateToString(DateUtils.dayTimePattern, new Date());
		String userName = UserUtils.getName();
		String status = lifeCherish.getCurrentStatus();
		/*根据状态值的改变，记录操作记录*/
		if (lifeCherish.getDelStatus().equals("1")) {
			if (status.equals("2")) {
				lifeCherish.setOperation(lifeCherish.getOperation()+"<b>"+time+", 由 "+userName+" 完成了实施计划;</b><br/>");
			} else if (status.equals("3")) {
				lifeCherish.setOperation(lifeCherish.getOperation()+"<b>"+time+", 由 "+userName+" 完成了工程设计;</b><br/>");
			} else if (status.equals("4") && lifeCherish.getRefuseStatus().equals("0")) {
				lifeCherish.setOperation(lifeCherish.getOperation()+"<b>"+time+", 由 "+userName+" 完成了工程实施;</b><br/>");
			} else if (status.equals("4") && lifeCherish.getRefuseStatus().equals("1")) {
				lifeCherish.setOperation(lifeCherish.getOperation()+"<b>"+time+", 由 "+userName+" 拒绝了项目的验收，原因：<span style='color:red'>"+lifeCherish.getRemark()+"</span>;</b><br/>");
				lifeCherish.setRemark("");
			} else if (status.equals("5")) {
				lifeCherish.setOperation(lifeCherish.getOperation()+"<b>"+time+", 由 "+userName+" 申请项目验收;</b><br/>");
			}
		} else if (lifeCherish.getDelStatus().equals("2")) {
			lifeCherish.setOperation(lifeCherish.getOperation()+"<b>"+time+", 由 "+userName+" 验收了该项目;</b><br/>");
		}
		lifeCherishService.saveLifeCherish(lifeCherish);
		return lifeCherish;
	}
	
	/**
	 * 
	 * step:
	 * 适用:iframe显示请求文件上传流程及步骤
	 * @param lifeCherish
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("step") 
	public String step(@ModelAttribute LifeCherish lifeCherish) {
		return "afgc/lifeCherishStep";
	}
	
	/**
	 * 
	 * getLifeCherishs:
	 * 适用: gis接口   根据年份和工程等级得到相应的数据列表
	 * @param request
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("lifeCherishList")
	@ResponseBody
	public List<LifeCherish> getLifeCherishs(HttpServletRequest request) {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		List<LifeCherish> lifeCherishs = lifeCherishService.getLifeCherishs(queryFilter);
		return lifeCherishs;
	}
	
	/**
	 * 
	 * getYears:
	 * 适用:gis获取安全生命防护工程排查时间的年份信息
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("years")
	@ResponseBody
	public List<String> getYears() {
		return lifeCherishService.getLifeCherishsYears();
	}
	
	/**
	 * 
	 * statisticsData:
	 * 适用:跳转安防工程统计页面
	 * @param model
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("statistics")
	public String statisticsData(Model model){
		List<String> list = lifeCherishService.getLifeCherishsYears();
		model.addAttribute("list", list);
		return "tjfx/afgc_statistics";
	}
	
	/**
	 * 
	 * getCountJson:
	 * 适用:统计某年度下面各状态和各等级的工程数量数据
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value = "countJson", method=RequestMethod.POST)
	public List<Map<String, Object>> getCountJson() {
		String year = SpringMvcUtils.getParameter("year");
		return lifeCherishService.getCountJson(year);
	}
	
	/**
	 * 
	 * exportData:
	 * 适用:将统计页面的表格中数据以excel的形式导出
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping("export")
	public ResponseEntity<byte[]> exportData() throws Exception {
		String year = SpringMvcUtils.getParameter("year");
		return lifeCherishService.exportData(year);
	}
	
	/**
	 * 
	 * statisticsAttachment:
	 * 适用:跳转到日常监管统计页面
	 * @param model
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("statAtt")
	public String statisticsAttachment(Model model){
		List<String> list = systemManageService.getAttachmentYears();
		model.addAttribute("list", list);
		return "tjfx/att_statistics";
	}
	
	/**
	 * 
	 * attCountJson:
	 * 适用:统计某年度下面各业务模块对应的文件数量
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value = "attCountJson", method=RequestMethod.POST)
	public List<Map<String, Object>> attCountJson() {
		String year = SpringMvcUtils.getParameter("year");
		return systemManageService.getCountJson(year);
	}
	
	@ResponseBody
	@RequestMapping("attExport")
	public ResponseEntity<byte[]> attExportData() throws Exception {
		String year = SpringMvcUtils.getParameter("year");
		return systemManageService.exportData(year);
	}
}
